#include "pcap.h"
#include "unistd.h"
#include "fcntl.h"
#include "sys/stat.h"
#include "sys/types.h"

#include "common.h"

#include "unet.h"
#include "ufile.h"
#include "ustring.h"
#include "pthread.h"

using namespace std;

pthread_mutex_t	lock;
pthread_cond_t	cond;
time_t stat_ = 0;

void *load_conf(void *arg)
{
	char *fname = (char*)arg;
	pthread_mutex_lock(&lock);
	cout<<"load files"<<endl;
	struct stat st;
	stat(fname, &st);
	if(stat_ == st.st_mtime){
		cout<<"stat not change"<<endl;
		pthread_cond_wait(&cond, &lock);
	}
	printf("load configure: %s\n", get_conf(fname, "as"));
	pthread_mutex_unlock(&lock);
}

void *checkstat(void *arg)
{
	time_t tm = time(NULL);
	struct stat st;
	char *fname = (char*)arg;
	pthread_mutex_lock(&lock);
	 
		stat(fname, &st);
	
}
int main(int argc, char **argv)
{
	char errBuf[PCAP_ERRBUF_SIZE], *devStr;
	//struct sockaddr_in sin;
	//int ret;

	/* get a device */
	devStr = pcap_lookupdev(errBuf);

	if(devStr)
	{
		printf("success: device: %s\n", devStr);
	}
	else
	{
		printf("error: %s\n", errBuf);
		exit(1);
	}

	/* open a device, wait until a packet arrives */
	pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf);

	if(!device)
	{
		printf("error: pcap_open_live(): %s\n", errBuf);
		exit(1);
	}

	/* wait loop forever */
	//int id = 0;
	//pcap_loop(device, -1, getPacket, (u_char*)&id);
	//
	//pcap_close(device);
	
	//int sockfd = 0;
	//sockfd = socket(AF_INET, SOCK_STREAM, 0);
	//if(sockfd < 0)
	//{
	//	printf("error: socket(): %s\n", strerror(sockfd));
	//	return 1;
	//}
	//sin.sin_family = AF_INET;
	//sin.sin_port = htons(9000);
    //
	//ret = inet_pton(AF_INET, "127.0.0.1", &(sin.sin_addr.s_addr));
	//if(1 != ret)
	//{
	//	printf("error:	inet_pton(): error code %d\n", ret);
	//	return 1;
	//}
	//
	//ret = connect(sockfd, (struct sockaddr*)&sin, sizeof(sin));
	//if(ret){
	//	cout<<"error: connect()"<<endl;
	//	exit(1);
	//}

	//cout<<"connected"<<endl;
    //
	//const char *buf = "hello world";
	//ret = write(sockfd, buf, strlen(buf));
    //
	//cout<<"write buf: "<<buf<<", "<<ret<<endl;

	//restream("./tcp_fin.bin", 0, 1);
	
	struct stat st;
	stat(argv[1], &st);
	printf("atime: %lu\n", st.st_atime);
	printf("mtime: %lu\n", st.st_mtime);
	printf("ctime: %lu\n", st.st_ctime);
	printf("%s\n", get_conf(argv[1], "as"));
	double s =2.1;
	cout<<2.1*3<<endl;
	return 0;
}
